
//获取当前天时间段
export function getnewDat(){
  var date = new Date();
  var nian = date.getFullYear(); //获取完整的年份(4位)
  var yue = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
  if(yue<10){
    yue = "0"+yue
  }
  var ri = date.getDate(); //获取当前日(1-31)
  if(ri<10){
    ri = "0"+ri
  }
  var kaishi = nian+'-'+yue+'-'+ri+' '+'00:00:00'
  var jieshu = nian+'-'+yue+'-'+ri+' '+'23:59:59'
  var arr=[]
  arr.push(kaishi)
  arr.push(jieshu)
  return arr
}
//获取当周 第一天 和 最后 一天
export function getCurrWeek(){
  var d = new Date();
  var year = d.getFullYear();
  var month = parseInt(d.getMonth())+1;

  //获取周一
  var beginWhereTheTime = GetMondayAndSunday(d);
  var dd = new Date(beginWhereTheTime);
  //获取周天
  dd.setDate(dd.getDate()+6);
  var monthSunday = parseInt(dd.getMonth())+1;
  var endWhereTheTime = dd.getFullYear() + "-" + formatDate(monthSunday) + "-" + formatDate(dd.getDate());
  var kaishi = beginWhereTheTime +' '+'00:00:00'
  var jieshu = endWhereTheTime +' '+'23:59:59'
  var arr=[]
  arr.push(kaishi)
  arr.push(jieshu)
  return arr
}
//获取指定日期所在周周一
function GetMondayAndSunday(dd) {
  var week = dd.getDay(); //获取时间的星期数
  var minus = week ? week - 1 : 6;
  dd.setDate(dd.getDate() - minus); //获取minus天前的日期
  var y = dd.getFullYear();
  var m = dd.getMonth() + 1; //获取月份
  var d = dd.getDate();
  return y + "-" + formatDate(m) + "-" + formatDate(d);
}
//格式化月和日为MM、dd
function formatDate(value){
  if(value < 10){
    value = "0" + value;
  }
  return value;
}
//获取当前月的第一天和最后一天
export function getCurrentMonthFirst(){
  // 最后一天
  var date=new Date();
  var currentMonth=date.getMonth();
  var nextMonth=++currentMonth;
  var nextMonthFirstDay=new Date(date.getFullYear(),nextMonth,1);
  var oneDay=1000*60*60*24;
  var lastTime = new Date(nextMonthFirstDay-oneDay);
  var month = parseInt(lastTime.getMonth()+1);
  var day = lastTime.getDate();
  if (month < 10) {
    month = '0' + month
  }
  if (day < 10) {
    day = '0' + day
  }
  var jieshu = date.getFullYear() + '-' + month + '-' + day+' '+'23:59:59';

  var dateks = new Date();
  dateks.setDate(1);
  var monthks = parseInt(date.getMonth()+1);
  var dayks = date.getDate();
  if (monthks < 10) {
    monthks = '0' + monthks
  }
  if (dayks < 10) {
    dayks = '0' + dayks
  }
  // 第一天
  var kaishi = date.getFullYear() + '-' + monthks + '-' + '01'+' '+'00:00:00';
  var arr=[]
  arr.push(kaishi)
  arr.push(jieshu)
  return arr
}

//数组去重
export function deteleObject(data){
  let hash = {};
  const data2 = data.reduce((preVal, curVal) => {
    hash[curVal.goodsId] ? '' : hash[curVal.goodsId] = true && preVal.push(curVal);
    return preVal
  }, [])
  return data2
}
//采购计划去重
export function deteleObjectJH(data){
  let hash = {};
  const data2 = data.reduce((preVal, curVal) => {
    hash[curVal.purchaseCode] ? '' : hash[curVal.purchaseCode] = true && preVal.push(curVal);
    return preVal
  }, [])
  return data2
}



//采购单子表-删除记录
export function DeleteJL(index,row,cSArr,jlArr){
  console.log('回显id',row.huiId)
  if(row.huiId){//如果有回显id 就记录
    row.type = 'delete'
    jlArr.push(row)
  }
  console.log('记录数据',jlArr)

  cSArr.splice(index,1)
  if(cSArr.length<1){
    cSArr.push(
      { goodsId:'',salesUnit:'',purchasePlanNumber:'',goodsSpecs:'',remark:'',planCount:'',sumPrice:''},
    )
  }
}

//采购单-下拉聚焦
export function focusXL(index){
  localStorage.setItem('xiabiao',index);
}

//采购单-下拉选择
export function selectCG(item,cSArr){
  console.log('选择商品',item);
  var xiabiao = localStorage.getItem('xiabiao')
  cSArr[xiabiao].goodsId = item.goodsId
  cSArr[xiabiao].goodsName = item.goodsName
  cSArr[xiabiao].purchaseUnit = item.purchaseUnit
  cSArr[xiabiao].marketPrice = item.marketPrice//采购金额
  cSArr[xiabiao].goodsWeight = item.goodsWeight//规格
  cSArr[xiabiao].purchasePlanNumber = item.purchasePlanNumber
  cSArr[xiabiao].goodsUnit = item.goodsUnit
  cSArr[xiabiao].goodsSpecs = item.goodsSpecs
  cSArr[xiabiao].planCount = item.planCount//计划采购数量
  cSArr[xiabiao].putStorageCode = item.putStorageCode//原料入库批次
  cSArr[xiabiao].storageCount = item.storageCount//原料数量
  cSArr[xiabiao].storagePrice = item.storagePrice//原料总价
  cSArr[xiabiao].url = ''//原料总价
  cSArr[xiabiao].inventory = item.inventory//库存


}
//记录数据
export function jilushuju(ymArr,jlArr){
  for(var i=0;i<ymArr.length;i++){
    if(ymArr[i].huiId){
      ymArr[i].type='update'
      jlArr.push(ymArr[i])
    }else {
      if(ymArr[i].id){//有商品id
        ymArr[i].type='add'
        jlArr.push(ymArr[i])
      }

    }
  }
}





// 首字母大小
export function titleCase(str) {
  return str.replace(/( |^)[a-z]/g, L => L.toUpperCase())
}

// 下划转驼峰
export function camelCase(str) {
  return str.replace(/-[a-z]/g, str1 => str1.substr(-1).toUpperCase())
}

